package com.sakura.mall.order.refundInfo.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.refundInfo.domain.entity.RefundInfoEntity;
import com.sakura.mall.order.refundInfo.domain.request.AddRefundInfoRequest;
import com.sakura.mall.order.refundInfo.domain.request.UpdateRefundInfoRequest;
import com.sakura.mall.order.refundInfo.domain.request.RefundInfoPageRequest;
import com.sakura.mall.order.refundInfo.domain.vo.RefundInfoVo;
import com.sakura.mall.order.refundInfo.mapper.RefundInfoMapper;
import com.sakura.mall.order.refundInfo.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 退款信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfoEntity> implements RefundInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddRefundInfoRequest request) {
        // todo 其他校验逻辑
        RefundInfoEntity refundInfo = request.convert(RefundInfoEntity.class);
        this.save(refundInfo);
    }

    @Override
    public Page<RefundInfoVo> queryPage(RefundInfoPageRequest request) {
        IPage<RefundInfoEntity> qp = request.toPage();
        LambdaQueryWrapper<RefundInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<RefundInfoVo> ret = qp.convert(entity -> entity.convert(RefundInfoVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateRefundInfoRequest request) {
        RefundInfoEntity entity = request.convert(RefundInfoEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

